package org.example.Aspect;

import net.bytebuddy.asm.Advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.example.common.annotation.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.NamedThreadLocal;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect
{
    /**这行代码是 SLF4J（Simple Logging Facade for Java，Java 简单日志门面） 框架的标准日志对象初始化方式，*/
    private static final Logger log = LoggerFactory.getLogger(LogAspect.class);

    /** 排除敏感属性字段 */
    public static final String[] EXCLUDE_PROPERTIES = { "password", "oldPassword", "newPassword", "confirmPassword" };

    /**计算操作耗时*/
    private static final ThreadLocal<Long> TIME_THREADLOCAL =new NamedThreadLocal<Long>("Cost Time");

    /**
     * 处理请求前执行,先记录时间
     */
    @Before(value = "@annotation(controllerLog)")
    public void doBefore(JoinPoint joinPoint, Log controllerLog){
        TIME_THREADLOCAL.set(System.currentTimeMillis());
    }

    /**
     * 处理完请求后执行
     * @param joinPoint 切点
     * @param controllerLog
     * @param jsonResult
     */
    @AfterReturning(value = "@annotation(controllerLog)",returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint,Log controllerLog,Object jsonResult){
        handleLog(joinPoint,controllerLog,null,jsonResult);
    }

    /**
     * 拦截异常操作
     * @param joinPoint 切点
     * @param controllerLog
     * @param e 错误
     */
    @AfterThrowing(value = "@annotation(controllerLog)",throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint,Log controllerLog,Exception e){
        handleLog(joinPoint,controllerLog,e,null);
    }

    /**
     * 写日志的方法
     * @param joinPoint
     * @param controllerLog
     * @param e
     * @param jsonResult
     */
    private void handleLog(JoinPoint joinPoint,Log controllerLog,Exception e,Object jsonResult){


    }


}